package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Command;
import model.IAction;
import model.MyObj;
import model.Position;
import utilities.GameResource;

public class ListScr extends Screen {

    static ListScr instance;

    public static ListScr gI() {
        if (instance == null) {
            instance = new ListScr();
        }
        return instance;
    }
    Command cmdClose;
    public int i;
    private Vector m_items = null;
    public Screen backScreen;
    public String m_strTitle;

    public void setListItem(Vector items) {
        m_items = items;
        init();
    }

    public void switchToMe(String title, Command leftCmd, Command centerCmd, Command rightCmd) {
        m_selected = 0;

        m_cmdLeft = null;
        m_cmdLeft = leftCmd;

        if(rightCmd == null)
        	m_cmdRight = cmdClose;
        else
        	m_cmdRight = rightCmd;
        
        m_cmdCenter = null;
        m_cmdCenter = centerCmd;
        backScreen = GameCanvas.currentScreen;
        
        m_strTitle = title;
        
        
        super.switchToMe();
    }
    public void setSelected(int se){
    	if(se != m_selected)
    		super.setSelected(se);
    	else if(m_cmdCenter != null)
    		m_cmdCenter.action.perform();
    }
    public void init() {
        Scroll.gI().init(m_height - 50, m_items.size() * 40, CameraList.cmy);
        GameCanvas.cameraList.setInfo(m_left, m_top + 40, m_width, 40, m_width, m_items.size() * 40,
                m_width, m_height - 50, m_items.size());
    }

    public ListScr() {
        m_width = GameCanvas.w - 100;
        if(GameCanvas.w <= 240)
        	m_width = 160;
        m_height = GameCanvas.h - 100;
        if(GameCanvas.h <= 200)
        	m_height = 160;
        m_left = (GameCanvas.w - m_width) / 2;
        m_top = (GameCanvas.h - m_height) / 2;
    	
        cmdClose = new Command(GameResource.close, new IAction() {
            public void perform() {
                close();
            }
        });
    }
    
    public void paint(Graphics g) {
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        backScreen.paint(g);
       
        PaintPopup.paintBoxTab(g, m_strTitle, m_left, m_top, m_width, m_height, 0x9be5ff, 0x3c8dc0);
               
        paintList(g);
        super.paint(g);
    }
    private void paintList(Graphics g) {
    	g.translate(0, m_top);
        
        Scroll.gI().paintScroll(g, m_left + m_width - 10, 30);
        Position pos = GameCanvas.getMinMaxFor(CameraList.cmy, 36, m_items.size(), 1, GameCanvas.h - 80);
        int y = 0;
        y += 36 * (pos.x + 1);

        g.setClip(m_left, 30, m_width, m_height - 50);
        
        g.translate(0, -CameraList.cmy);

        for (i = pos.x; i < pos.y; i++) {
            if (i == m_selected) {
                PaintPopup.paintSelect(g, m_left + 10, y, m_width - 20, 36);
            }
            
            MyObj ava = (MyObj) m_items.elementAt(i);
            ava.paintInRow(g, m_left + 10, y, m_width - 20, 36, i == m_selected);

            y += 40;
        }
        pos = null;
    }

    public void updateKey() {
        super.updateKey();
    }

    public void addItem(MyObj obj) {
        m_items.addElement(obj);
        init();
    }

    public void removeItem(MyObj obj) {
        m_items.removeElement(obj);
        init();
    }

    public int getItemSize() {
        return m_items.size();
    }

    public void update() {
        backScreen.update();
        Scroll.gI().updateScroll(CameraList.cmy, CameraList.cmtoY);
    }

    public MyObj getSelectedItem() {
        if (m_selected >= 0 && m_selected < m_items.size()) {
            return (MyObj) m_items.elementAt(m_selected);
        }
        return null;
    }

    public MyObj getItem(int id) {
        int a = m_items.size();
        for (int i = 0; i < a; i++) {
            MyObj obj = (MyObj) m_items.elementAt(i);
            if (obj.m_iId == id) {
                return obj;
            }
        }
        return null;
    }

    public void close() {
        // TODO Auto-generated method stub
        m_cmdCenter = null;
        m_cmdRight = null;
        m_cmdLeft = null;
        GameCanvas.cameraList.close();

        backScreen.switchToMe();
      //  System.gc();
    }
    
    public void paintMarqueeMsg(Graphics g){
	}
}